package com.chlitina.store.modules.cash.model;

import java.util.List;

public class ShoppingPayment {
	private ProductsPayment productsPayment = new ProductsPayment();
	private ProductsPayment caresPayment = new ProductsPayment();

	public ProductsPayment getProductsPayment() {
		return productsPayment;
	}

	public void updatePaymentForCares(MemberShoppingCart shoppingCart) {
		float totalPay = 0;
		float cashPay = 0;
		float cardAllPay = 0;
		float cardTimesPay = 0;
		CarePayment payment = null;
		for (StoreCareCart cart : shoppingCart.getSystemCares()) {
			totalPay += cart.getRealCostTotal();
			payment = cart.getPayment();
			if (payment.getDurattionCardPaymentDetail() != null) {
				// cardDurationPay += 0;
			} else if (payment.getTimesCardPaymentDetail() != null) {
				cardTimesPay += payment.getTimesCardPaymentDetail()
						.getRealCost();
			} else if (payment.getAllCardPaymentDetail() != null) {
				cardAllPay += payment.getAllCardPaymentDetail().getRealCost();
			} else {
				cashPay = payment.getCash();
			}
		}

		for (StoreCareCart cart : shoppingCart.getStoreCares()) {
			totalPay += cart.getRealCostTotal();
			payment = cart.getPayment();
			if (payment.getDurattionCardPaymentDetail() != null) {
				// cardDurationPay += 0;
			} else if (payment.getTimesCardPaymentDetail() != null) {
				cardTimesPay += payment.getTimesCardPaymentDetail()
						.getRealCost();
			} else if (payment.getAllCardPaymentDetail() != null) {
				cardAllPay += payment.getAllCardPaymentDetail().getRealCost();
			} else {
				cashPay = payment.getCash();
			}
		}
		
         //solutionCares
		for (StoreCareCart cart : shoppingCart.getSolutionCares()) {
			totalPay += cart.getRealCostTotal();
			payment = cart.getPayment();
			if (payment.getDurattionCardPaymentDetail() != null) {
				// cardDurationPay += 0;
			} else if (payment.getTimesCardPaymentDetail() != null) {
				cardTimesPay += payment.getTimesCardPaymentDetail()
						.getRealCost();
			} else if (payment.getAllCardPaymentDetail() != null) {
				cardAllPay += payment.getAllCardPaymentDetail().getRealCost();
			} else {
				cashPay = payment.getCash();
			}
		}
		caresPayment.setTotalPay(totalPay);
		caresPayment.setCardAllPay(cardAllPay);
		caresPayment.setCardTimesPay(cardTimesPay);
		caresPayment.setCashPay(cashPay);
	}

	public void updatePaymentForProduct(MemberShoppingCart shoppingCart) {
		float totalPay = 0;
		float cashPay = 0;
		float cardAllPay = 0;
		float cardProductPay = 0;
		ProductPayment payment = null;
		List<StoreProductCart> storeProducts = shoppingCart.getStoreProducts();
		for (StoreProductCart cart : storeProducts) {
			totalPay += cart.getRealCostTotal();
			payment = cart.getPayment();
			if (payment.getProductCardPaymentDetail() != null) {
				cardProductPay += payment.getProductCardPaymentDetail()
						.getRealCost();
			} else if (payment.getAllCardPaymentDetail() != null) {
				cardAllPay += payment.getAllCardPaymentDetail().getRealCost();
			} else {
				cashPay = payment.getCash();
			}
		}
		
		List<StoreProductCart> systemProducts = shoppingCart
				.getSystemProducts();
		for (StoreProductCart cart : systemProducts) {
			totalPay += cart.getRealCostTotal();
			payment = cart.getPayment();
			if (payment.getProductCardPaymentDetail() != null) {
				cardProductPay += payment.getProductCardPaymentDetail()
						.getRealCost();
			} else if (payment.getAllCardPaymentDetail() != null) {
				cardAllPay += payment.getAllCardPaymentDetail().getRealCost();
			} else {
				cashPay = payment.getCash();
			}
		}
		
		//solution products
		List<SolutionStoreProductCart> solutionProducts = shoppingCart.getSolutionProducts();
		for (StoreProductCart cart : solutionProducts) {
			totalPay += cart.getRealCostTotal();
			payment = cart.getPayment();
			if (payment.getProductCardPaymentDetail() != null) {
				cardProductPay += payment.getProductCardPaymentDetail()
						.getRealCost();
			} else if (payment.getAllCardPaymentDetail() != null) {
				cardAllPay += payment.getAllCardPaymentDetail().getRealCost();
			} else {
				cashPay = payment.getCash();
			}
		}
		
		productsPayment.setTotalPay(totalPay);
		productsPayment.setCardAllPay(cardAllPay);
		productsPayment.setCardProductPay(cardProductPay);
		productsPayment.setCashPay(cashPay);
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("total " + total + ", pay " + pay + ", debt " + debt);
		return sb.toString();
	}

	private float total;
	private float pay;
	private float debt;

	public float getTotal() {
		return total;
	}

	public void setTotal(float total) {
		this.total = total;
	}

	public float getPay() {
		return pay;
	}

	public void setPay(float pay) {
		this.pay = pay;
	}

	public float getDebt() {
		return debt;
	}

	public void setDebt(float debt) {
		this.debt = debt;
	}

	public ProductsPayment getCaresPayment() {
		return caresPayment;
	}
}
